//
//  ViewController.m
//  14-CollectionViewTest
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "ImageCollectionViewCell.h"
#import "LabelCollectionReusableView.h"
#import "LabelAndCollectionViewReusableView.h"
#define kCollectionReusableViewHeaderNoCell @"header"
#define kCollectionViewReuse1 @"reuse"
#define kCollectionViewReusableViewHeader @"headerReuse"
#define kArcRandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (CGRectGetWidth(self.view.bounds) - 10) / 3;
    //item大小
    layout.itemSize = CGSizeMake(width, width);
     // 列间距
    layout.minimumInteritemSpacing = 0;
   // 行间距
    layout.minimumLineSpacing = 5;
   //头大小
    layout.headerReferenceSize = CGSizeMake(0, 50);
    
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse1];
    
    [collectionView registerClass:[LabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusableViewHeader];
    
    [collectionView registerClass:[LabelAndCollectionViewReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusableViewHeaderNoCell];
//    1.通过上边距实现视图添加（UICollectionView 本质是UIScrollView）
    collectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -300, self.view.bounds.size.width, 290)];
    view.backgroundColor = [UIColor greenColor];
    [collectionView addSubview:view];
    
   //2.自定义头视图
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
        
    } else {
        return CGSizeMake(0, 50);
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return  2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 100;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse1 forIndexPath:indexPath];
    cell.contentView.backgroundColor = kArcRandomColor;
    
    return  cell;
}
//collectionView头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LabelAndCollectionViewReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionReusableViewHeaderNoCell forIndexPath:indexPath];
        
        return view;
    } else {
        LabelCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusableViewHeader forIndexPath:indexPath];
//    reusableView.backgroundColor = [UIColor redColor];
        return reusableView;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
